<?php
/**
 * Category.class.php
 *
 * @author camfee <camfee@foxmail.com>
 * @date   18-1-2 下午9:17
 */

namespace Center;

use Common\ModuleBase;
use Common\Bridge;

class Category extends ModuleBase
{
    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'Category',
        'fields' => [
            'Id' => self::VAR_TYPE_KEY,
            'ParentId' => self::VAR_TYPE_INT,
            'Name' => self::VAR_TYPE_STRING,
            'IconUrl' => self::VAR_TYPE_STRING,
            'Sort' => self::VAR_TYPE_INT,
            'UpdateTime' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        // 可选, MC KEY, "KeyName:%d", %d会用主键ID替代
        'mckey' => 'Category:%d',
        // 可选, 超时时间, 默认不过期
        'mctime' => 86400,
    ];

    const MC_CATEGORY_LIST = 'All_Category_List';
    const UPDATE_DEL_CACHE_LIST = true; // 更新是否清除列表缓存
    // 列表缓存
    protected static $_cache_list_keys = [
        self::MC_CATEGORY_LIST => self::CACHE_LIST_TYPE_MC
    ];
    // 新增必须字段 field => 1
    protected static $_add_must_fields = [
        'Name' => 1,
    ];

    /**
     * 获取所有分类
     *
     * @return array
     */
    public static function getAllCate($cache = true)
    {
        if ($cache) {
            $type = parent::MOD_TYPE_MEMCACHE;
        } else {
            $type = parent::MOD_TYPE_DB;
        }
        $extra = [
            'type' => $type,
            'mckey' => self::MC_CATEGORY_LIST,
            'mtime' => 86400,
            'fields' => '*',
            'listkey' => 'Id',
            'order' => 'Sort DESC',
        ];
        $ret = parent::getDataByFields([], $extra);

        return $ret;
    }

    /**
     * @see ModuleBase::add() 新增
     * @see ModuleBase::update() 更新
     * @see ModuleBase::getInfoByIds() 按id查询
     * @see ModuleBase::getList() 条件查询
     * @see ModuleBase::delete() 删除
     */

}